//
//  ZYLFindBackCollectionView.m
//  Cartoon
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZYLFindBackCollectionView.h"
#import "Define.h"
#import "ZYLFindBackCollectionViewCell.h"
#import "ZYLFindBanners.h"
@interface ZYLFindBackCollectionView ()<ChangeMoveView, ChangeColor>

@property (nonatomic, retain) UIView *backView;
@property (nonatomic, retain) UIView *moveView;
@property (nonatomic, retain) UICollectionReusableView *recordHeader;
@property (nonatomic, retain) ZYLFindBackCollectionViewCell *recordCell;
@property (nonatomic, assign) NSInteger num;
@end


@implementation ZYLFindBackCollectionView
- (void)dealloc
{
    [_recordHeader release];
    [_recordCell release];
    [_moveView release];
    [_backView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self createCollection];
        
    }
    return self;
}

#pragma mark - 创建collectionView
- (void)createCollection
{
    self.num = 0;
    self.bounces = NO;
    self.showsVerticalScrollIndicator = NO;
    CGFloat h = ([[self.topic_List[0] topics] count] / 2) * (MYHEIGHT + 5) + 55;
    ((UICollectionViewFlowLayout *)self.collectionViewLayout).itemSize = CGSizeMake(SCREEN_WIDTH, h);
    ((UICollectionViewFlowLayout *)self.collectionViewLayout).headerReferenceSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT / 3 + 30);
    self.backgroundColor = [UIColor whiteColor];
    [self registerClass:[ZYLFindBackCollectionViewCell class] forCellWithReuseIdentifier:@"backCollectionView"];
    [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerImage"];
    
}





- (void)setTopic_List:(NSMutableArray *)topic_List
{
    if (_topic_List != topic_List) {
        [_topic_List release];
        _topic_List = [topic_List retain];
    }
    [self reloadData];
}
#pragma mark - header设置
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerImage" forIndexPath:indexPath];
        header.backgroundColor = [UIColor whiteColor];
        self.recordHeader = header;
        if (self.backView == nil) {
            ZYLFindBanners *banners = [[[ZYLFindBanners alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3) arr:self.bannerArr] autorelease];
            [header addSubview:banners];
            [header addSubview:banners.page];
            self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 3, SCREEN_WIDTH, 30)];
            self.backView.backgroundColor = [UIColor whiteColor];
            [header addSubview:self.backView];
            UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
            v.backgroundColor = [UIColor lightGrayColor];
            [self.backView addSubview:v];
            self.moveView = [[UIView alloc] initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH / 4.0, 5)];
            self.moveView.layer.cornerRadius = 2.5;
            self.moveView.backgroundColor = [UIColor colorWithRed:0.0 green:0.4549 blue:0.9137 alpha:1];
            [self.backView addSubview:self.moveView];
            NSArray *arr = @[@"专", @"单", @"新", @"荐"];
            NSArray *color = @[[UIColor colorWithRed:0.0 green:0.4549 blue:0.9137 alpha:1], [UIColor colorWithRed:0.9333 green:0.2471 blue:0.2392 alpha:1], [UIColor colorWithRed:1.0 green:0.698 blue:0.2196 alpha:1], [UIColor colorWithRed:0.2275 green:0.6667 blue:0.2980 alpha:1]];
            for (NSInteger i = 0; i < 4; i ++) {
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                
                button.frame = CGRectMake(SCREEN_WIDTH / 4.0 * i, 0.5, SCREEN_WIDTH / 4.0, 25);
                button.tag = 1000 + i;
                if (i == 0) {
                    button.selected = YES;
                }
                [button setTitle:arr[i] forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:20];
                [button setTitleColor:color[i] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
                [self.backView addSubview:button];
            }
        }
        return header;
    }
    return nil;
}

#pragma mark - 头部四个button的点击方法
- (void)buttonAction:(UIButton *)button
{
    if (button.selected == YES) {
        return;
    }
    for (UIButton *b in self.backView.subviews) {
        if ([b class] == [UIButton class]) {
            b.selected = NO;
        }
    }
    button.selected = YES;
    self.recordCell.myScrollCount = button.tag - 1000;
        [UIView animateWithDuration:0.3 animations:^{
            self.moveView.frame = CGRectMake(375 / 4.0 * (button.tag - 1000), 25, 375 / 4.0, 5);
            self.moveView.backgroundColor = button.titleLabel.textColor;
        }];
    [self reloadData];
    [self reloadScroll];
    
}

#pragma mark - 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYLFindBackCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"backCollectionView" forIndexPath:indexPath];
    self.recordCell = cell;
    cell.topic_List = self.topic_List;
    cell.moveViewDelegate = self;
    cell.colorDelegate = self;
    return cell;
}

#pragma mark - section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark - item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

#pragma mark - 设置collectionView边界
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma mark - 设置item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h = ([[self.topic_List[self.num] topics] count] / 2) * (MYHEIGHT + 5) + 60;
    if (self.num == 1) {
        h = ([[self.topic_List[self.num] comics] count] / 2) * (MYSIGLEHEIGHT + 5) + 60;
    }
    NSLog(@"%f, %ld", h, [[self.topic_List[0] topics] count]);
    return CGSizeMake(SCREEN_WIDTH, h);
}
#pragma mark - moveView移动/改变颜色
- (void)changeMoveView:(CGFloat)f
{
    CGFloat x1 = f * SCREEN_WIDTH;
    
    self.moveView.frame = CGRectMake(x1, 25, SCREEN_WIDTH / 4.0, 5);
    if (x1 <= SCREEN_WIDTH / 4.0) {
        self.moveView.backgroundColor = [UIColor colorWithRed:(0.9333 * (f * 4)) green:(0.4549 + ((0.2471 - 0.4549) * (f * 4))) blue:(0.9137 + ((0.2392 - 0.9137) * (f * 4))) alpha:1];
        
    } else if (x1 <= SCREEN_WIDTH / 4.0 * 2) {
        
        self.moveView.backgroundColor = [UIColor colorWithRed:(0.9333 + ((1.0 - 0.9333) * ((f - 0.25) * 4))) green:(0.2471 + ((0.698 - 0.2471) * ((f - 0.25) * 4))) blue:(0.2392 + ((0.2196 - 0.2392) * ((f - 0.25) * 4))) alpha:1];
        
    } else if (x1 <= SCREEN_WIDTH / 4.0 * 3) {
        self.moveView.backgroundColor = [UIColor colorWithRed:(1 + ((0.2275 - 1)*(f - 0.5) * 4)) green:(0.458 + ((0.6667 - 0.458) * ((f - 0.5) * 4))) blue:(0.0196 + ((0.2980 - 0.0196) * ((f - 0.5) * 4))) alpha:1];
    }
    [self reloadScroll];
}
#pragma mark - 四个button始终显示在屏幕上方
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= SCREEN_HEIGHT / 3) {
        self.backView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 30);
        [self.superview addSubview:self.backView];
    }
    if (scrollView.contentOffset.y < SCREEN_HEIGHT / 3) {
        self.backView.frame = CGRectMake(0, SCREEN_HEIGHT / 3, SCREEN_WIDTH, 30);
        [self.recordHeader addSubview:self.backView];
    }

}
#pragma mark - 切换时回到top
- (void)reloadScroll
{
    if (self.contentOffset.y >= SCREEN_HEIGHT / 3) {
        self.contentOffset = CGPointMake(self.contentOffset.x, SCREEN_HEIGHT / 3);
    }
}
#pragma mark - 改变颜色结束后
- (void)changeColor:(CGFloat)f picNum:(NSInteger)picNum
{
    self.num = f * 4;
    CGFloat x1 = f * SCREEN_WIDTH;
    for (UIButton *b in self.backView.subviews) {
        if ([b class] == [UIButton class]) {
            b.selected = NO;
        }
    }
    NSInteger x2 = x1 / (SCREEN_WIDTH / 4.0);
    self.recordCell.myScrollCount = x2;
    ((UIButton *)[self.backView viewWithTag:(x2 + 1000)]).selected = YES;
    [self reloadData];
}
@end
